package org.itboys.mobile.service.mongo.web;


import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.util.NameUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_用户_service
 */
@Service
public class SundayMemberService extends BaseMongoService<SundayMember> {

    @Autowired
    private ResourceHolder resourceHolder;
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<SundayMember> getEntityClass() {
        return SundayMember.class;
    }
    /**
     * 分页获取用户信息
     * @param request
     * @return
     */
    public PageResult<SundayMember> selectMember(HttpServletRequest request){
        Map<String,Object> param =ParamUtil.packageMongoExactParam(request,"userType","status","parentId");
        param.put("isDeleted", 0);
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"nickName","mobileName","mobile");

        return super.containpageQuery(request,containsparam,param);
    }
    /**
     * 分页获取用户信息
     * @param request
     * @return
     */
    public List<SundayMember> selectMemberNoPage(HttpServletRequest request,List<Integer> userTypes){
        Map<String,Object> param =ParamUtil.packageMongoExactParam(request,"userType");
        param.put("isDeleted", 0);
        String parentIdStr=request.getParameter("parentId");
         if(StringUtils.isNotEmpty(parentIdStr)){
             param.put("parentId",Long.valueOf(parentIdStr));
         }else{
             param.put("parentId",0);
         }

        if(ListUtil.isNotNull(userTypes)){
            param.put("userType in",userTypes);
        }

        return super.list(param);
    }

    /**
     * 修改或新增
     * @param member
     */
    public void saveMember(SundayMember member){
        Map<String,Object> param =new HashMap<String, Object>();
        if(member.getId()==0l){

            param.put("isDeleted",0);
            param.put("mobile",member.getMobile());
            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("手机号不能重复");
            }
            //生成默认头像
            member.setImage(NameUtil.getRandomLogo());
            if(StringUtils.isNotEmpty(member.getPassword())){
                member.setPassword(Digests.md5(member.getPassword()));
            }
            //2017年8月27日
            member.setIsSuper(0);
            super.save(member);
        }else{
            param.put("id !=",member.getId());
            param.put("isDeleted",0);
            param.put("mobile",member.getMobile());
            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("手机号不能重复");
            }
            if(StringUtils.isNotEmpty(member.getPassword())){
                member.setPassword(Digests.md5(member.getPassword()));
            }
            super.updateExceptEmpty(member.getId(),member);
        }
    }

    /**
     *  单个用户查询
     * @param id
     * @param userType
     * @return
     */
    public SundayMember findOne(Long id,Integer userType) {
        SundayMember member =null;
        if(LongUtil.isNotZreo(id)){
            member=super.getById(id);


        }else{
            member=new SundayMember();
            member.setId(0);
            member.setUserType(userType);

        }
        member.setPassword(null);
        return member;
  }
    /**
     * 重置为默认密码
     * @param id
     */
    public void resetPassword(Long id){
            super.update(id,"password",Digests.md5(SundayCommonConstants.default_password));

    }
    /**
     * 冻结或启用用户账号
     * @param id
     */
    public void changeStatus(Long id,Integer status){
        super.update(id,"status",status);
    }


}
